﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using DevExpress.XtraEditors;
using Smart.Data.Helper;
using System.Data.SqlClient;
using Smart.Utils.Security2;
using System.IO;
using System.Data;
using System.Windows.Forms;

namespace Smart.Net.Info
{
    public class MyInfo
    {
        private static string m_Address;
        private static string m_Company;
        private static string m_Company_Id;
        private static string m_Email;
        private static string m_Fax;
        private static string m_Licence;
        private static Image m_Photo;
        private static string m_Tax;
        private static string m_Tel;
        private static string m_WebSite;
        public MyInfo()
        {
        }

        public MyInfo(string Company_Id, string Company, string Address, string Tel, string Fax, string WebSite, string Email, string Tax, string Licence, Image Photo)
        {
            m_Company_Id = Company_Id;
            m_Company = Company;
            m_Address = Address;
            m_Tel = Tel;
            m_Fax = Fax;
            m_WebSite = WebSite;
            m_Email = Email;
            m_Tax = Tax;
            m_Licence = Licence;
            m_Photo = Photo;
        }

        public static bool Delete(SqlTransaction myTransaction, string Company_Id)
        {
            string[] myParams = new string[] { "@Company_Id" };
            object[] myValues = new object[] { Company_Id };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(MyInfo.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "SYS_COMPANY_Delete", myParams, myValues) == "OK");
        }

        private static void DispError(object sender, SqlHelperException e)
        {
            XtraMessageBox.Show(e.Message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public static string Get(string Company_Id)
        {
            string str = "";
            string[] myParams = new string[] { "@Company_Id" };
            object[] myValues = new object[] { Company_Id };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("SYS_COMPANY_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_Company_Id = Convert.ToString(reader["Company_Id"]);
                    m_Company = Convert.ToString(reader["Company"]);
                    m_Company = MyEncryption.Decrypt(m_Company, "123654", true);
                    m_Address = Convert.ToString(reader["Address"]);
                    m_Tel = Convert.ToString(reader["Tel"]);
                    m_Fax = Convert.ToString(reader["Fax"]);
                    m_WebSite = Convert.ToString(reader["WebSite"]);
                    m_Email = Convert.ToString(reader["Email"]);
                    m_Tax = Convert.ToString(reader["Tax"]);
                    m_Licence = Convert.ToString(reader["Licence"]);
                    if (!reader.IsDBNull(reader.GetOrdinal("Photo")) && (reader["Photo"] != Convert.DBNull))
                    {
                        byte[] buffer = (byte[])reader["Photo"];
                        if (buffer.Length != 2)
                        {
                            MemoryStream stream = new MemoryStream(buffer);
                            m_Photo = Image.FromStream(stream);
                        }
                        if (buffer.Length == 2)
                        {
                            m_Photo = null;
                        }
                    }
                    str = "OK";
                }
                reader.Close();
                helper.Close();
                reader = null;
            }
            return str;
        }

        public static string GetInfo()
        {
            return Get(MyEncryption.Encrypt("ERP01", "123654", true));
        }

        public static bool Insert(SqlTransaction myTransaction, string Company_Id, string Company, string Address, string Tel, string Fax, string WebSite, string Email, string Tax, string Licence, object Photo)
        {
            string[] myParams = new string[] { "@Company_Id", "@Company", "@Address", "@Tel", "@Fax", "@WebSite", "@Email", "@Tax", "@Licence", "@Photo" };
            object[] myValues = new object[] { Company_Id, Company, Address, Tel, Fax, WebSite, Email, Tax, Licence, Photo };
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(MyInfo.DispError);
            return (helper.ExecuteNonQuery(myTransaction, "SYS_COMPANY_Insert", myParams, myValues) == "OK");
        }

        public static void SetData(string Company_Id, string Company, string Address, string Tel, string Fax, string WebSite, string Email, string Tax, string Licence, Image Photo)
        {
            m_Company_Id = Company_Id;
            m_Company = Company;
            m_Address = Address;
            m_Tel = Tel;
            m_Fax = Fax;
            m_WebSite = WebSite;
            m_Email = Email;
            m_Tax = Tax;
            m_Licence = Licence;
            m_Photo = Photo;
        }

        public static bool Update()
        {
            return Update(m_Company_Id, m_Company, m_Address, m_Tel, m_Fax, m_WebSite, m_Email, m_Tax, m_Licence, m_Photo);
        }

        public static bool Update(string Company_Id, string Company, string Address, string Tel, string Fax, string WebSite, string Email, string Tax, string Licence, object Photo)
        {
            SqlHelper helper = new SqlHelper();
            helper.Error += new SqlHelper.ErrorEventHander(MyInfo.DispError);
            if ((helper.Open() == "OK") && Update(helper.Connection, Company_Id, Company, Address, Tel, Fax, WebSite, Email, Tax, Licence, Photo))
            {
                helper.Close();
                return true;
            }
            return false;
        }

        public static bool Update(SqlConnection myConnection, string Company_Id, string Company, string Address, string Tel, string Fax, string WebSite, string Email, string Tax, string Licence, object Photo)
        {
            string[] myParams = new string[] { "@Company_Id", "@Company", "@Address", "@Tel", "@Fax", "@WebSite", "@Email", "@Tax", "@Licence" };
            object[] myValues = new object[] { Company_Id, Company, Address, Tel, Fax, WebSite, Email, Tax, Licence };
            SqlHelper helper = new SqlHelper();
            helper.CommandType = CommandType.Text;
            helper.Error += new SqlHelper.ErrorEventHander(MyInfo.DispError);
            return (helper.ExecuteNonQuery(myConnection, "UPDATE [SYS_COMPANY] SET [Company] = @Company, [Address] = @Address, [Tel] = @Tel, [Fax] = @Fax, [WebSite] = @WebSite, [Email] = @Email, [Tax] = @Tax, [Licence] = @Licence Where  [Company_Id] = @Company_Id", myParams, myValues) == "OK");
        }
        
        public static string Address
        {
            get
            {
                return m_Address;
            }
            set
            {
                m_Address = value;
            }
        }

        public static string Company
        {
            get
            {
                return m_Company;
            }
            set
            {
                m_Company = value;
            }
        }

        public static string Company_Id
        {
            get
            {
                return m_Company_Id;
            }
            set
            {
                m_Company_Id = value;
            }
        }

        public string Copyright
        {
            get
            {
                return "C\x00c3\x00b4ng Ty Ph\x00e1\x00ba\x00a7n M\x00e1\x00bb\x0081m Ho\x00c3\x00a0n H\x00e1\x00ba\x00a3o";
            }
        }

        public static string Email
        {
            get
            {
                return m_Email;
            }
            set
            {
                m_Email = value;
            }
        }

        public static string Fax
        {
            get
            {
                return m_Fax;
            }
            set
            {
                m_Fax = value;
            }
        }

        public static string Licence
        {
            get
            {
                return m_Licence;
            }
            set
            {
                m_Licence = value;
            }
        }

        public static Image Photo
        {
            get
            {
                return m_Photo;
            }
        }

        public string ProductName
        {
            get
            {
                return "Class clsGetInfo";
            }
        }

        public string ProductVersion
        {
            get
            {
                return "1.0.0.0";
            }
        }

        public static string Tax
        {
            get
            {
                return m_Tax;
            }
            set
            {
                m_Tax = value;
            }
        }

        public static string Tel
        {
            get
            {
                return m_Tel;
            }
            set
            {
                m_Tel = value;
            }
        }

        public static string WebSite
        {
            get
            {
                return m_WebSite;
            }
            set
            {
                m_WebSite = value;
            }
        }
    }
}
